package com.cskaoyan.market.admin.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.cskaoyan.market.admin.service.MarketAdminAdService;
import com.cskaoyan.market.db.domain.MarketAd;
import com.cskaoyan.market.db.domain.MarketAdExample;
import com.cskaoyan.market.db.mapper.MarketAdMapper;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;

/**
 * @author James
 * @since 2023/03/21 16:54
 */

public class MarketAdminAdServiceImpl implements MarketAdminAdService {
    @Override
    public List<MarketAd> list(int page, int limit, String name, String content, String sort, String order) {

        SqlSession session = MyBatisUtil.getSession();
        MarketAdMapper adMapper = session.getMapper(MarketAdMapper.class);
        MarketAdExample marketAdExample = new MarketAdExample();
        MarketAdExample.Criteria criteria = marketAdExample.createCriteria();
        // 如果不为空,则要给匹配上
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtil.isEmpty(content)) {
            criteria.andContentLike("%" + content + "%");
        }
        criteria.andDeletedEqualTo(false);
        // 设置排序规则
        marketAdExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        List<MarketAd> adList = adMapper.selectByExample(marketAdExample);
        session.commit();
        session.close();
        return adList;
    }

    @Override
    public MarketAd create(String content, boolean enabled, String link, String name, Byte position, String url) {

        SqlSession session = MyBatisUtil.getSession();
        MarketAdMapper mapper = session.getMapper(MarketAdMapper.class);

        MarketAd marketAd = new MarketAd();
        marketAd.setContent(content);
        marketAd.setEnabled(enabled);
        marketAd.setLink(link == null ? "" : link);
        marketAd.setName(name);
        marketAd.setPosition(position);
        marketAd.setUrl(url);
        marketAd.setAddTime(LocalDateTime.now());
        marketAd.andLogicalDeleted(false);
        marketAd.setUpdateTime(LocalDateTime.now());

        mapper.insert(marketAd);
        session.commit();
        session.close();
        return marketAd;
    }

    @Override
    public MarketAd update(String addTime, String content, Boolean deleted, Boolean enabled, Integer id, String link,
        String name, Byte position, String updateTime, String url) {

        SqlSession session = MyBatisUtil.getSession();
        MarketAdMapper mapper = session.getMapper(MarketAdMapper.class);
        mapper.selectByPrimaryKey(id);

        MarketAd marketAd = new MarketAd();
        marketAd.setAddTime((((LocalDateTime.parse(addTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))))));
        marketAd.setContent(content);
        marketAd.setDeleted(deleted);
        marketAd.setEnabled(enabled);
        marketAd.setId(id);
        marketAd.setLink(link);
        marketAd.setName(name);
        marketAd.setPosition(position);
        marketAd.setUpdateTime(LocalDateTime.now());
        marketAd.setUrl(url);

        mapper.updateByPrimaryKey(marketAd);
        session.commit();
        session.close();
        return marketAd;

    }

    @Override
    public void delete(String addTime, String content, Boolean delete, Boolean enabled, int id, String link,
        String name, Byte position, String updateTime, String url) {

        SqlSession session = MyBatisUtil.getSession();
        MarketAdMapper mapper = session.getMapper(MarketAdMapper.class);
        MarketAd marketAd = mapper.selectByPrimaryKey(id);
        marketAd.setUpdateTime(LocalDateTime.now());
        marketAd.andLogicalDeleted(true);
        marketAd.setDeleted(true);

        mapper.updateByPrimaryKey(marketAd);
        session.commit();
        session.close();

    }
}